<template>
  <Transition name="toast">
    <div 
      v-if="visible"
      class="message-toast"
      :class="type"
    >
      <Icon :icon="icon" />
      <span>{{ message }}</span>
    </div>
  </Transition>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, onBeforeUnmount } from 'vue'
import { Icon } from '@iconify/vue'

const props = defineProps<{
  message: string
  type?: 'success' | 'error' | 'info' | 'warning'
  duration?: number
}>()

const emit = defineEmits<{
  (e: 'destroy'): void
}>()

const visible = ref(false)
let timer: number | null = null

const icon = computed(() => {
  switch (props.type) {
    case 'success':
      return 'mdi:check-circle'
    case 'error':
      return 'mdi:alert-circle'
    case 'warning':
      return 'mdi:alert'
    default:
      return 'mdi:information'
  }
})

onMounted(() => {
  visible.value = true
  if (props.duration !== 0) {
    timer = window.setTimeout(() => {
      visible.value = false
      setTimeout(() => emit('destroy'), 300)
    }, props.duration || 3000)
  }
})

onBeforeUnmount(() => {
  if (timer) {
    clearTimeout(timer)
  }
})
</script>

<style lang="scss" scoped>
.message-toast {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  padding: 12px 24px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.75);
  color: white;
  display: flex;
  align-items: center;
  gap: 8px;
  z-index: 1000;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  backdrop-filter: blur(4px);

  &.success {
    background: rgba(82, 196, 26, 0.9);
  }

  &.error {
    background: rgba(255, 77, 79, 0.9);
  }

  &.warning {
    background: rgba(250, 173, 20, 0.9);
  }

  .iconify {
    font-size: 20px;
  }
}

.toast-enter-active,
.toast-leave-active {
  transition: all 0.3s ease;
}

.toast-enter-from,
.toast-leave-to {
  opacity: 0;
  transform: translate(-50%, -20px);
}
</style> 